<?php namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;

/**
 * 作品类别
 * Class WorksType
 *
 * @package App\Models
 * @property int $id
 * @property string $name 名称
 * @property string $code 代码
 * @property int $max_authors 作者人数上限
 * @property int $series_num 系列数量
 * @property string $upload_exts 允许上传的扩展名
 * @property string $created_at
 * @property string $updated_at
 * @property string $deleted_at
 * @property int $show_barcode 显示条码下载
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\PropositionCompany[] $propositionCompanies
 * @method static \Illuminate\Database\Query\Builder|\App\Models\WorksType whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\WorksType whereName($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\WorksType whereCode($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\WorksType whereMaxAuthors($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\WorksType whereSeriesNum($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\WorksType whereUploadExts($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\WorksType whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\WorksType whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\WorksType whereDeletedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Models\WorksType whereShowBarcode($value)
 * @mixin \Eloquent
 */
class WorksType extends Model
{
    use SoftDeletes;

    protected $table = 'works_types';

    protected $dates = ['deleted_at'];

    protected $fillable = [
        'name',
        'code',
        'max_authors',
        'series_max',
        'series_min',
        'catalog_min',
        'catalog_max',
        'upload_exts',
        'show_barcode',
        'templet',
        'max_teachers'
    ];

    /**
     * 允许上传的扩展名
     * @return array
     */
    public static function getAvailableUploadExts()
    {
        return [
            'jpg'  => 'jpg',
            'jpeg' => 'jpeg',
            'gif'  => 'gif',
            'png'  => 'png',
            'avi'  => 'avi',
            'wmv'  => 'wmv',
            'mp3'  => 'mp3',
            'mp4'  => 'mp4',
            'flv'  => 'flv',
            'tiff' => 'tiff',
            'pdf'  => 'pdf',
            'doc'  => 'doc',
            'docx' => 'docx',
            'swf'  => 'swf',
            'ppt'  => 'ppt',
            'pptx' => 'pptx',
            'txt'  => 'txt',
            'url'  => '外部链接',
            'idea' => '创意按钮',
        ];
    }

    /**
     * 允许上传的文件扩展名名称
     * @return array
     */
    public function getUploadExtNames()
    {
        $names = [];
        $uploadExts = self::getAvailableUploadExts();
        $exts = explode(',', $this->upload_exts);
        foreach ($exts as $ext) {
            if (isset($uploadExts[$ext])) {
                $names[] = $uploadExts[$ext];
            }
        }
        return $names;
    }

    /**
     * 用于select的列表
     * @return array
     */
    public static function getListData()
    {
        $infos = self::all();
        $listData = ['0' => '请选择'];
        foreach ($infos as $info) {
            $listData[$info->id] = $info->name;
        }
        return $listData;
    }

    /**
     * 对应的命题企业
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function propositionCompanies()
    {
        return $this->belongsToMany(PropositionCompany::class, 'company_works_type', 'works_type_id', 'company_id');
    }
}